// pages/dailyScreen/dailyScreen.js
const httpRequest = require('../../utils/http_utils.js')
const dateutil = require('../../utils/date_util.js')
const app = getApp();
// var Cache = new Array();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '搜索',
    isShow: false, //历史任务筛选
    dailyShow: false, //日常巡检筛选
    val: '', //输入框value
    PageSize: 15, //单页数据条数
    CurrentPage: 1, //当前页码
    IsEquipRoom: 0, //全部任务
    TaskName: '', //任务名称
    Inspectstatus: 0, //巡检状态
    HistoryList: [], //搜索集合
    totalNum: 0, //数据总条数
    showIs: false, //暂无图片
    downloading: false, //加载中
    noMore: false, //没有更多了
    loadingFailed: false, //数据加载失败
    height: 0, //动态高度
    confirm: 0, //判断是日常巡检还是历史任务
    history_Cache: [], //本地缓存
    checked: false, //点击选中


    areaList: [], //区域
    buildingList: [], //建筑
    floorList: [], //楼层
    roomList: [], //房间

    area: '区域',
    building: '',
    floor: '',
    room: '',
    areaGuid: '', //区域唯一标识
    buildingGuid: '', //建筑唯一标识
    floorGuid: '', //楼层唯一标识
    roomGuid: '', //房间唯一标识

    checkarea: '',
    checkbuilding: '',
    checkfloor: '',
    checkroom: '',

    isarea: true,
    isbuilding: false,
    isfloor: false,
    isroom: false,

    showarea: false, //是否显示区域列表
    showbuilding: false, //是否显示建筑列表
    showfloor: false, //是否显示楼层列表
    showroom: false, //是够显示房间列表

    area_float: 'left',
    building_float: 'left',
    floor_float: 'left',
    room_float: 'left',
    all_height:'auto',

    maskFlag: false, //是否显示遮罩

    Masks: 4, //遮罩高度

    areaGuid: '', //区域Guid
    buildingGuid: '', //建筑Guid
    floorGuid: '', //楼层Guid
    rooomGuid: '', //房间Guid
    startDate: '', //任务开始时间
    FinishDate: '', //任务结束时间
    cacheName: '', //点击历史搜索内容
    positionTop:0, //滚动条位置
  },

  //跳转详情
  JumpDetails: function(data) {
    if (this.data.confirm == 1 || this.data.confirm == '1') {
      wx.navigateTo({
        url: '../xunJianDetails/xunJianDetails?TaskType=' + data.currentTarget.dataset.tasktype + '&RowGuid=' + data.currentTarget.dataset.rowguid + '&RoomName=' + data.currentTarget.dataset.roomname + '&IsAlarm=' + data.currentTarget.dataset.isalarm + '&TaskRemark=' + data.currentTarget.dataset.taskremark,
      })
    }

  },

  //关联建筑
  setarea(e) {
    this.setData({
      checkarea: e.currentTarget.dataset.area,
      areaGuid: e.currentTarget.dataset.guid,
      buildingGuid: '', //建筑Guid
      floorGuid: '', //楼层Guid
      rooomGuid: '', //房间Guid
      HistoryList: [], //列表集合
      isarea: false,
      isbuilding: true,
      isfloor: false,
      isroom: false,
      showarea: true,
      showbuilding: true,
      showfloor: false,
      showroom: false,
      area_float: 'left',
      building_float: 'left',
      building: '建筑',
      all_height:'600rpx',
    })
    this.getBuilding();
  },

  //关联楼层
  setbuilding(e) {
    this.setData({
      checkbuilding: e.currentTarget.dataset.building,
      buildingGuid: e.currentTarget.dataset.guid,
      HistoryList: [], //列表集合
      isarea: false,
      isbuilding: false,
      isfloor: true,
      isroom: false,
      // showarea: false,
      showarea: true,
      showbuilding: true,
      showfloor: true,
      showroom: false,
      building_float: 'left',
      // floor_float: 'right',
      floor_float: 'left',
      floor: '楼层',
      all_height:'600rpx',
    })
    this.getFloor();
  },

  //关联房间
  setfloor(e) {
    this.setData({
      checkfloor: e.currentTarget.dataset.floor,
      floorGuid: e.currentTarget.dataset.guid,
      HistoryList: [], //列表集合
      isarea: false,
      isbuilding: false,
      isfloor: false,
      isroom: true,
      // showarea: false,
      showarea: true,
      // showbuilding: false,
      showbuilding: true,
      showfloor: true,
      showroom: true,
      floor_float: 'left',
      // room_float: 'right',
      room_float: 'left',
      room: '房间',
      all_height:'600rpx',
    })
    this.getRoom();
  },

  setroom(e) {
    this.setData({
      checkroom: e.currentTarget.dataset.room,
      rooomGuid: e.currentTarget.dataset.guid,
      HistoryList: [], //列表集合
    })
    this.initData();
    // setTimeout(() => {
    //   this.setData({
    //     maskFlag: false,
    //     area: '区域',
    //     building: '',
    //     floor: '',
    //     room: '',
    //     showarea: false,
    //     showbuilding: false,
    //     showfloor: false,
    //     showroom: false,
    //   })
    //   ////////////////////////////////////////////////查询筛选数据
    // }, 1000)    
  },

  //重置区域
  selectarea() {
    this.setData({
      showarea: true,
      showbuilding: false,
      showfloor: false,
      showroom: false,
      area: '区域',
      building: '',
      floor: '',
      room: '',
      isarea: true,
      isbuilding: false,
      isfloor: false,
      isroom: false,
      area_float: 'left',
      maskFlag: true,
      all_height:'600rpx',
    })
    if (this.data.maskFlag == true) {
      this.setData({
        Masks: 65
      })
    }
  },

  //重置建筑
  selectbuilding() {
    this.setData({
      showarea: true,
      showbuilding: true,
      showfloor: false,
      showroom: false,
      floor: '',
      room: '',
      isarea: false,
      isbuilding: true,
      isfloor: false,
      isroom: false,
      area_float: 'left',
      // building_float: 'right',
      building_float: 'left',
      all_height:'600rpx',
    })
    this.getFloor();
  },

  //重置楼层
  selectfloor() {
    this.setData({
      showarea: false,
      showbuilding: true,
      showfloor: true,
      showroom: false,
      room: '',
      isarea: false,
      isbuilding: false,
      isfloor: true,
      isroom: false,
      building_float: 'left',
      floor_float: 'left',
      all_height:'600rpx',
    })
  },

  selectroom() {
    this.setData({
      showarea: false,
      showbuilding: false,
      showfloor: false,
      showroom: true,
      isarea: false,
      isbuilding: false,
      isfloor: false,
      isroom: true,
      all_height:'600rpx',
    })
  },

  //隐藏遮罩
  hidemask() {
    this.setData({
      maskFlag: false,
      area: '区域',
      building: '',
      floor: '',
      room: '',
      showarea: false,
      showbuilding: false,
      showfloor: false,
      showroom: false,
      all_height:0,
    })
    if (this.data.maskFlag == false) {
      this.setData({
        Masks: 4
      })
    }
  },

  //获取所属区域
  getRegion: function() {
    var that = this
    that.setData({
      areaList: []
    })
    httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectRegionApi, null,
      function(res) {
        console.log('获取所属区域', JSON.stringify(res))
        if (res.length > 0) {
          var newArray = that.data.areaList
          for (var i = 0; i < res.length; i++) {
            newArray.push(res[i])
          }
          that.setData({
            areaList: newArray
          })
        }
      },
      function(err) {
        console.error(err)
      })
  },

  //获取所属建筑
  getBuilding: function() {
    // debugger
    var that = this
    that.setData({
      buildingList: []
    })
    var param = {
      RegionGuid: this.data.areaGuid
    };
    httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectBuildingApi, param,
      function(res) {
        console.log('获取所属建筑', JSON.stringify(res))
        if (res.length > 0) {
          var newArray = that.data.buildingList
          for (var i = 0; i < res.length; i++) {
            newArray.push(res[i])
          }
          that.setData({
            buildingList: newArray
          })
        }
        that.initData();
      },
      function(err) {
        console.error(err)
      })
  },

  //获取所属楼层
  getFloor: function() {
    var that = this
    that.setData({
      floorList: []
    })
    var param = {
      BuildingGuid: that.data.buildingGuid
    };
    console.log('获取所属楼层', JSON.stringify(param))
    httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectFloorApi, param,
      function(res) {
        console.log('获取所属楼层', JSON.stringify(res))
        if (res.length > 0) {
          var newArray = that.data.floorList
          for (var i = 0; i < res.length; i++) {
            newArray.push(res[i])
          }
          that.setData({
            floorList: newArray
          })
        }
        that.initData();
      },
      function(err) {
        console.error(err)
      })
  },

  //获取所属房间
  getRoom: function() {
    var that = this
    that.setData({
      roomList: []
    })
    var param = {
      FloorGuid: that.data.floorGuid
    };
    console.log('获取所属房间', JSON.stringify(param))
    httpRequest.GetValidateRequest(httpRequest.apiInfo.Project_GetProjectRoomApi, param,
      function(res) {
        console.log(JSON.stringify(res))
        if (res.length > 0) {
          var newArray = that.data.roomList
          for (var i = 0; i < res.length; i++) {
            newArray.push(res[i])
          }
          that.setData({
            roomList: newArray
          })
        }
        that.initData();
      },
      function(err) {
        console.error(err)
      })
  },

  //////////////////////////////四级联动end///////////////////////////////////////

  //获取输入框的值
  inputInfo: function(e) {
    var that = this;
    if (that.data.confirm == 1 || that.data.confirm == '1') {
      // that.setData({
      //   PageSize: 15,
      //   CurrentPage: 1,
      //   Inspectstatus: 0,
      //   IsEquipRoom: 0,
      //   HistoryList: [],
      //   TaskName: e.detail.value
      // })
      return
    } else {
      if (e.detail.value == '' || e.detail.value == null || e.detail.value == undefined) {
        that.setData({
          HistoryList: []
        })
      } else {
        that.setData({
          PageSize: 15,
          CurrentPage: 1,
          IsEquipRoom: 0,
          HistoryList: [],
          TaskName: e.detail.value
        })
        that.initData();
      }
    }
  },

  Eliminates: function() {
    this.setData({
      val: '',
      HistoryList: []
    })
  },

  initData: function() {
    // debugger;
    var that = this;
    if (that.data.confirm == 1 || that.data.confirm == '1') {
      console.log("------------------------历史任务----------------------")
      var param = {
        PageSize: that.data.PageSize,
        CurrentPage: that.data.CurrentPage,
        IsEquipRoom: that.data.IsEquipRoom,
        TaskName: that.data.TaskName,
        Inspectstatus: that.data.Inspectstatus,
        LocalGuid: "",
        startDate: that.data.startDate,
        FinishDate: that.data.FinishDate
      }
      console.log(JSON.stringify(param) + "--------------------历史任务上传参数")
      httpRequest.PostValidateRequest(httpRequest.apiInfo.GetHistoryInspectTaskListMobileApi, param,
        function(data) {
          console.log(JSON.stringify(data))
          var historyArrays = that.data.HistoryList;
          if (data.pageData == null || data.pageData == [] || data.pageData == undefined) {
            that.setData({
              showIs: true,
              noMore: false
            })
          } else {
            for (var i = 0; i < data.pageData.length; i++) {
              var params = {
                TaskName: data.pageData[i].TaskName,
                Location: data.pageData[i].Location,
                IsAlarm: data.pageData[i].IsAlarm,
                InspectDate: dateutil.dateFormat4(data.pageData[i].InspectDate),
                TaskType: data.pageData[i].TaskType,
                RowGuid: data.pageData[i].RowGuid,
                RoomName: data.pageData[i].RoomName,
                TaskRemark: data.pageData[i].TaskRemark,
              }
              historyArrays.push(params);
            }
            that.setData({
              HistoryList: historyArrays,
              totalNum: data.totalNum,
              uploading: false,
              noMore: false,
              showIs: false
            })
          }

        },
        function(err) {

        })
    } else {
      console.log("------------------------日常巡检----------------------")
      var CurrentParam = {
        PageSize: that.data.PageSize,
        CurrentPage: that.data.CurrentPage,
        IsEquipRoom: that.data.IsEquipRoom,
        TaskName: that.data.TaskName,
        RegionGuid: that.data.areaGuid,
        BuildGuid: that.data.buildingGuid,
        FloorGuid: that.data.floorGuid,
        RoomGuid: that.data.rooomGuid
      }
      console.log(CurrentParam);
      httpRequest.PostValidateRequest(httpRequest.apiInfo.GetInspectTaskListMobileApi, CurrentParam,
        function(data) {
          // debugger
          console.log(that.data.PageSize);
          console.log(JSON.stringify(data))
          var HistoryArray = that.data.HistoryList;
          if (data.pageData == null || data.pageData == [] || data.pageData == undefined) {
            that.setData({
              showIs: true,
              noMore: false
            })
          } else {
            console.log(data.pageData.length + "_____________数据长度")

            for (var i = 0; i < data.pageData.length; i++) {
              var params = {
                TaskName: data.pageData[i].TaskName,
                Location: data.pageData[i].Location,
                TaskEndTime: dateutil.dateFormat4(data.pageData[i].TaskEndTime),
                IsAlarm: data.pageData[i].IsAlarm,
              }
              HistoryArray.push(params)
            }
            that.setData({
              HistoryList: HistoryArray,
              totalNum: data.totalNum,
              uploading: false,
              noMore: false,
              showIs: false
            })
          }

        },
        function(err) {

        })
    }
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    console.log(that.data.totalNum + "_______总条数")
    var pages = Math.round((that.data.totalNum / 15) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.initData();
    }
  },

  Enter: function(e) { //回车事件
    var that = this;
    if (that.data.confirm == 1 || that.data.confirm == '1') {
      that.setData({
        cacheName: ''
      })
      console.log(e.detail.value + "---------------回车")
      var Cache = that.data.history_Cache;
      let param = {
        id: Cache.length + 1,
        name: e.detail.value,
        checked: false
      }

      for (var i = 0; i < Cache.length; i++) {
        if (Cache[i].name == param.name) {
          Cache.splice(i, 1)
        }
      }

      Cache.push(param);
      console.log(Cache.length)
      if (Cache.length > 6) {
        Cache.splice(0, 1)
      }
      if (Cache.length < 1) {
        that.setData({
          // height: 140
          height: ((40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 80,
        })
      } else if (that.data.history_Cache.length < 4) {
        that.setData({
          // height: 180
          height: ((40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 160,
        })
      } else {
        that.setData({
          // height: 220
          height: ((40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 250,
        })
      }
      that.setData({
        history_Cache: Cache,
        TaskName: e.detail.value,
        PageSize: 15,
        CurrentPage: 1,
        HistoryList: [],
        positionTop: 0
      })
      that.initData();
      wx.setStorageSync("history", that.data.history_Cache)
    } else {
      return;
    }

  },

  searchHistory: function() {
    console.log(JSON.stringify(wx.getStorageSync('history')) + "-----------------历史")
    console.log(wx.getStorageSync('history').length)
    if (wx.getStorageSync('history').length == 0) {

    } else {
      this.setData({
        history_Cache: wx.getStorageSync('history')
      })
      if (wx.getStorageSync('history').length < 4) {
        this.setData({
          // height: 180
          height: ((40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 160,
        })
      } else if (wx.getStorageSync('history').length > 3) {
        this.setData({
          // height: 220
          height: ((40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 250,
        })
      }
    }
    console.log(JSON.stringify(this.data.history_Cache) + "______________历史搜索")
  },

  //点击搜索历史
  searchchecked: function(e) {
    console.log(this.data.TaskName)
    console.log(e.currentTarget.dataset.name)
    // if (this.data.TaskName == e.currentTarget.dataset.name) {
    //   this.setData({
    //     TaskName: ''
    //   })
    // } else {
    this.setData({
      TaskName: e.currentTarget.dataset.name,
      cacheName: e.currentTarget.dataset.name,
      HistoryList: [],
      PageSize: 15, //单页数据条数
      CurrentPage: 1, //当前页码
      positionTop: 0
    })
    this.initData();
    // }

  },

  Eliminate_Cache: function() {
    var that = this;
    wx.removeStorage({
      key: 'history',
      success(res) {
        console.log(res)
        that.setData({
          history_Cache: [],
          // height: 140
          height: ((40 + app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 80,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(JSON.stringify(options))
    if (options.confirm == 1 || options.confirm == "1") {
      this.setData({
        isShow: true,
        dailyShow: false,
        confirm: 1
      })
      this.searchHistory();

    } else {
      this.setData({
        isShow: false,
        dailyShow: true,
        // height: 150,
        height: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 200,
        confirm: 0
      })
    }
    this.getRegion()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})